﻿using System;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.IDAL;
using System.Data.SqlClient;
using System.Data;
using OgilvyOne.Model;

namespace OgilvyOne.SqlServerDAL
{
    public class ActivityManageDal : IActivityManage
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        public ActivityManageDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int activityId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from COFCO_ACTIVITY");
            strSql.Append(" where ACTIVITY_ID=@ACTIVITY_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_ID", SqlDbType.Int,4)
};
            parameters[0].Value = activityId;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="activityname">活动名</param>
        /// <returns></returns>
        public bool Exists(string activityname, int activityId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from COFCO_ACTIVITY");
            strSql.Append(" where ACTIVITY_NAME=@ACTIVITY_NAME and ACTIVITY_ID not in ("+activityId+")");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_NAME",activityname)
            };
            return int.Parse(SQLHelper.ExecuteScalar(connstr, CommandType.Text, strSql.ToString(), parameters).ToString()) > 0;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ActivityManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into COFCO_ACTIVITY(");
            strSql.Append("ACTIVITY_NAME,START_DATE,END_DATE,ACTIVITY_ADDR,HOST_UNIT,ACTIVITY_CONTENT,STATE,ACTIVITY_TYPE,SOURCE,TITLE_IMG,IS_HOTS,HOTS_IMG,IS_KV,KV_IMG,ADDTIME,ADDUSERNAME)");
            strSql.Append(" values (");
            strSql.Append("@ACTIVITY_NAME,@START_DATE,@END_DATE,@ACTIVITY_ADDR,@HOST_UNIT,@ACTIVITY_CONTENT,@STATE,@ACTIVITY_TYPE,@SOURCE,@TITLE_IMG,@IS_HOTS,@HOTS_IMG,@IS_KV,@KV_IMG,@ADDTIME,@ADDUSERNAME)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@START_DATE", SqlDbType.DateTime),
					new SqlParameter("@END_DATE", SqlDbType.DateTime),
					new SqlParameter("@ACTIVITY_ADDR", SqlDbType.VarChar,200),
					new SqlParameter("@HOST_UNIT", SqlDbType.VarChar,200),
					new SqlParameter("@ACTIVITY_CONTENT", SqlDbType.Text),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@ACTIVITY_TYPE", SqlDbType.Int,4),
					new SqlParameter("@SOURCE", SqlDbType.VarChar,50),
					new SqlParameter("@TITLE_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@IS_HOTS", SqlDbType.Int,4),
					new SqlParameter("@HOTS_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@IS_KV", SqlDbType.Int,4),
					new SqlParameter("@KV_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@ADDTIME", SqlDbType.DateTime),
					new SqlParameter("@ADDUSERNAME", SqlDbType.VarChar,50)};
            parameters[0].Value = model.Activityname;
            parameters[1].Value = model.Startdate;
            parameters[2].Value = model.Enddate;
            parameters[3].Value = model.Activityaddr;
            parameters[4].Value = model.Hostunit;
            parameters[5].Value = model.Activitycontent;
            parameters[6].Value = model.State;
            parameters[7].Value = model.Activitytype;
            parameters[8].Value = model.Source;
            parameters[9].Value = model.Titleimg;
            parameters[10].Value = model.Ishots;
            parameters[11].Value = model.Hotsimg;
            parameters[12].Value = model.Iskv;
            parameters[13].Value = model.Kvimg;
            parameters[14].Value = model.Addtime;
            parameters[15].Value = model.Addusername;
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ActivityManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update COFCO_ACTIVITY set ");
            strSql.Append("ACTIVITY_NAME=@ACTIVITY_NAME,");
            strSql.Append("START_DATE=@START_DATE,");
            strSql.Append("END_DATE=@END_DATE,");
            strSql.Append("ACTIVITY_ADDR=@ACTIVITY_ADDR,");
            strSql.Append("HOST_UNIT=@HOST_UNIT,");
            strSql.Append("ACTIVITY_CONTENT=@ACTIVITY_CONTENT,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("ACTIVITY_TYPE=@ACTIVITY_TYPE,");
            strSql.Append("SOURCE=@SOURCE,");
            strSql.Append("TITLE_IMG=@TITLE_IMG,");
            strSql.Append("IS_HOTS=@IS_HOTS,");
            strSql.Append("HOTS_IMG=@HOTS_IMG,");
            strSql.Append("IS_KV=@IS_KV,");
            strSql.Append("KV_IMG=@KV_IMG,");
            strSql.Append("ADDTIME=@ADDTIME,");
            strSql.Append("ADDUSERNAME=@ADDUSERNAME");
            strSql.Append(" where ACTIVITY_ID=@ACTIVITY_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@START_DATE", SqlDbType.DateTime),
					new SqlParameter("@END_DATE", SqlDbType.DateTime),
					new SqlParameter("@ACTIVITY_ADDR", SqlDbType.VarChar,200),
					new SqlParameter("@HOST_UNIT", SqlDbType.VarChar,200),
					new SqlParameter("@ACTIVITY_CONTENT", SqlDbType.Text),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@ACTIVITY_TYPE", SqlDbType.Int,4),
					new SqlParameter("@SOURCE", SqlDbType.VarChar,50),
					new SqlParameter("@TITLE_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@IS_HOTS", SqlDbType.Int,4),
					new SqlParameter("@HOTS_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@IS_KV", SqlDbType.Int,4),
					new SqlParameter("@KV_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@ADDTIME", SqlDbType.DateTime),
					new SqlParameter("@ADDUSERNAME", SqlDbType.VarChar,50),
					new SqlParameter("@ACTIVITY_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Activityname;
            parameters[1].Value = model.Startdate;
            parameters[2].Value = model.Enddate;
            parameters[3].Value = model.Activityaddr;
            parameters[4].Value = model.Hostunit;
            parameters[5].Value = model.Activitycontent;
            parameters[6].Value = model.State;
            parameters[7].Value = model.Activitytype;
            parameters[8].Value = model.Source;
            parameters[9].Value = model.Titleimg;
            parameters[10].Value = model.Ishots;
            parameters[11].Value = model.Hotsimg;
            parameters[12].Value = model.Iskv;
            parameters[13].Value = model.Kvimg;
            parameters[14].Value = model.Addtime;
            parameters[15].Value = model.Addusername;
            parameters[16].Value = model.Activityid;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int activityId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_ACTIVITY ");
            strSql.Append(" where ACTIVITY_ID=@ACTIVITY_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_ID", SqlDbType.Int,4)
};
            parameters[0].Value = activityId;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }
        /// <summary>
        /// 删除一组数据
        /// </summary>
        public bool DeleteList(string activityIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_ACTIVITY ");
            strSql.Append(" where ACTIVITY_ID in (" + activityIdlist + ")  ");
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), null) > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ActivityManageModel GetModel(int activityId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ACTIVITY_ID,ACTIVITY_NAME,START_DATE,END_DATE,ACTIVITY_ADDR,HOST_UNIT,ACTIVITY_CONTENT,STATE,ACTIVITY_TYPE,SOURCE,TITLE_IMG,IS_HOTS,HOTS_IMG,IS_KV,KV_IMG,ADDTIME,ADDUSERNAME from COFCO_ACTIVITY ");
            strSql.Append(" where ACTIVITY_ID=" + activityId);
            OgilvyOne.Model.ActivityManageModel model = new OgilvyOne.Model.ActivityManageModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ACTIVITY_ID"].ToString() != "")
                {
                    model.Activityid = int.Parse(ds.Tables[0].Rows[0]["ACTIVITY_ID"].ToString());
                }
                model.Activityname = ds.Tables[0].Rows[0]["ACTIVITY_NAME"].ToString();
                if (ds.Tables[0].Rows[0]["START_DATE"].ToString() != "")
                {
                    model.Startdate = DateTime.Parse(ds.Tables[0].Rows[0]["START_DATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["END_DATE"].ToString() != "")
                {
                    model.Enddate = DateTime.Parse(ds.Tables[0].Rows[0]["END_DATE"].ToString());
                }
                model.Activityaddr = ds.Tables[0].Rows[0]["ACTIVITY_ADDR"].ToString();
                model.Hostunit = ds.Tables[0].Rows[0]["HOST_UNIT"].ToString();
                model.Activitycontent = ds.Tables[0].Rows[0]["ACTIVITY_CONTENT"].ToString();
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ACTIVITY_TYPE"].ToString() != "")
                {
                    model.Activitytype = int.Parse(ds.Tables[0].Rows[0]["ACTIVITY_TYPE"].ToString());
                }
                model.Source = ds.Tables[0].Rows[0]["SOURCE"].ToString();
                model.Titleimg = ds.Tables[0].Rows[0]["TITLE_IMG"].ToString();
                if (ds.Tables[0].Rows[0]["IS_HOTS"].ToString() != "")
                {
                    model.Ishots = int.Parse(ds.Tables[0].Rows[0]["IS_HOTS"].ToString());
                }
                model.Hotsimg = ds.Tables[0].Rows[0]["HOTS_IMG"].ToString();
                if (ds.Tables[0].Rows[0]["IS_KV"].ToString() != "")
                {
                    model.Iskv = int.Parse(ds.Tables[0].Rows[0]["IS_KV"].ToString());
                }
                model.Kvimg = ds.Tables[0].Rows[0]["KV_IMG"].ToString();
                if (ds.Tables[0].Rows[0]["ADDTIME"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(ds.Tables[0].Rows[0]["ADDTIME"].ToString());
                }
                model.Addusername = ds.Tables[0].Rows[0]["ADDUSERNAME"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ACTIVITY_ID,ACTIVITY_NAME,START_DATE,END_DATE,ACTIVITY_ADDR,HOST_UNIT,ACTIVITY_CONTENT,STATE,ACTIVITY_TYPE,SOURCE,TITLE_IMG,IS_HOTS,HOTS_IMG,IS_KV,KV_IMG,ADDTIME,ADDUSERNAME ");
            strSql.Append(" FROM COFCO_ACTIVITY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ACTIVITY_ID,ACTIVITY_NAME,START_DATE,END_DATE,ACTIVITY_ADDR,HOST_UNIT,ACTIVITY_CONTENT,STATE,ACTIVITY_TYPE,SOURCE,TITLE_IMG,IS_HOTS,HOTS_IMG,IS_KV,KV_IMG,ADDTIME,ADDUSERNAME ");
            strSql.Append(" FROM COFCO_ACTIVITY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "COFCO_ACTIVITY";
            parameters[1].Value = "ACTIVITY_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
